<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Insure;
use App\Models\Category;
use App\Models\InsureCompany;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class InsureController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Insure(['comp', 'category']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('coverage');
            $grid->column('target');
            $grid->column('is_recommend')->display(function ($status) {
                if ($status == 0) {
                    return "<span style='color:green'>不推荐</span>";
                }
                if ($status == 1) {
                    return "<span style='color:red'>推荐</span>";
                }
            });
            $grid->column('min_price');
            $grid->column('category.title', '保险分类');
            $grid->column('comp.name', '保险公司');
            $grid->column('image')->image('', 50, 50);
            $grid->column('sort', '排序');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->export();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('is_recommend')->select(['0'=>'不推荐', '1'=>'推荐']);
                $filter->equal('cid')->select(Category::orderBy('sort')->pluck('title', 'id'));
                $filter->equal('comp_id')->select(InsureCompany::pluck('name', 'id'));

            });

            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Insure(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('coverage');
            $show->field('target');
            $show->field('is_recommend');
            $show->field('min_price');
            $show->field('cid');
            $show->field('comp_id');
            $show->field('content');
            $show->field('image')->image();
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Insure(), function (Form $form) {
            $form->display('id');
            $form->text('title')->required();
            $form->image('image')->autoUpload()->uniqueName()->required();
            $form->text('coverage')->required();
            $form->text('target')->required();
            $form->select('is_recommend')->options(function (){
                return [0=>'否', 1=>'是'];
            });
            $form->text('min_price')->required();
            $form->select('cid')->options(function(){
                return Category::orderBy('sort')->pluck('title', 'id')->toArray();
            })->required();
            $form->select('comp_id', '保险公司')->options(function(){
                return InsureCompany::pluck('name', 'id')->toArray();
            })->required();
            $form->number('sort', '排序')->default(100000);
            $form->editor('content')->required();

            $form->display('created_at');
            $form->display('updated_at');

            $form->disableViewButton();
        });
    }
}
